using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Entities.Base;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 角色实体
    /// 用于定义系统中的用户角色和权限组合，支持多租户隔离
    /// </summary>
    [Table("roles")]
    public class Role : BaseEntity, ITenantEntity
    {
        /// <summary>
        /// 租户ID
        /// 用于多租户数据隔离
        /// </summary>
        [Required]
        [StringLength(36)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 角色名称
        /// 角色的显示名称
        /// </summary>
        [Required]
        [StringLength(100)]
        public string Name { get; set; } = string.Empty;
        
        /// <summary>
        /// 角色代码/标识符
        /// 系统中的唯一标识符，用于代码中引用
        /// </summary>
        [Required]
        [StringLength(50)]
        public string Code { get; set; } = string.Empty;
        
        /// <summary>
        /// 角色描述
        /// 对角色功能和职责的详细说明
        /// </summary>
        [Column(TypeName = "text")]
        public string? Description { get; set; }
        
        /// <summary>
        /// 角色级别/权重
        /// 数值越高权限越大，用于层级权限控制
        /// </summary>
        public int Level { get; set; } = 0;
        
        /// <summary>
        /// 是否系统内置角色
        /// 内置角色不可删除和修改
        /// </summary>
        public bool IsSystem { get; set; } = false;
        
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsActive { get; set; } = true;
        
        /// <summary>
        /// 数据权限范围
        /// 0=仅自己, 1=部门, 2=部门及下级, 3=全部
        /// </summary>
        public byte DataScope { get; set; } = 0;
        
        /// <summary>
        /// 排序权重
        /// </summary>
        public int SortOrder { get; set; } = 0;
        
        /// <summary>
        /// 备注信息
        /// </summary>
        [Column(TypeName = "text")]
        public string? Remark { get; set; }
        
        /// <summary>
        /// 创建人ID
        /// </summary>
        public long? CreatedBy { get; set; }
        
        /// <summary>
        /// 修改人ID
        /// </summary>
        public long? UpdatedBy { get; set; }
        
        // 导航属性

        /// <summary>
        /// 租户导航属性
        /// </summary>
        public Tenant? Tenant { get; set; }

        /// <summary>
        /// 角色权限关联集合
        /// </summary>
        [InverseProperty(nameof(RolePermission.Role))]
        public ICollection<RolePermission> RolePermissions { get; set; } = new List<RolePermission>();
        
        /// <summary>
        /// 用户角色关联集合
        /// </summary>
        [InverseProperty(nameof(UserRole.Role))]
        public ICollection<UserRole> UserRoles { get; set; } = new List<UserRole>();
        
        /// <summary>
        /// 创建人导航属性
        /// </summary>
        [ForeignKey(nameof(CreatedBy))]
        public User? Creator { get; set; }
        
        /// <summary>
        /// 修改人导航属性
        /// </summary>
        [ForeignKey(nameof(UpdatedBy))]
        public User? Updater { get; set; }
    }
}
